<template>
  <div>
    <el-header>
      <span>我的文件</span>
      <el-button @click="getFileInfo">刷新文件</el-button>
    </el-header>
    <el-table
      :data="tableData.filter(data => !search||data.timestamp.includes(search) ||data.originalname.toLowerCase().includes(search.toLowerCase()))"
      :default-sort="{prop:tableData.timestamp,order:'descending'}"
      style="width: 100%"
    >
      <el-table-column label="Date" prop="timestamp" sortable></el-table-column>
      <el-table-column label="FileName" prop="originalname" sortable></el-table-column>
      <el-table-column label="Owner" prop="owner" sortable></el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="getAccess(scope.$index, scope.row)">请求权限</el-button>
          <el-button size="mini" @click="getfile(scope.$index, scope.row)">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
const NodeRSA = require("node-rsa");
const crypto = require("crypto");
const BASE64 = "base64";
var token = localStorage.getItem("token");
export default {
  data() {
    return {
      tableData: [],
      search: "",
      myHeaders: { Authorization: token },
      fileInfo: {
        fileid: "0"
      },
      getAccessInfo:{
        fileid:"1",
        filename:"随笔.docx",
        access:"raw",
        validity:"2020-05-28 13:24:15",
        signedData:""
      }
    };
  },
  mounted: function() {
    this.getFileInfo();
  },
  methods: {
    submitUpload() {
      //此处必须打印一下token  否则取不到token
      // console.log(token);
      this.$refs.upload.submit();
    },
    getFileInfo() {
      this.$axios.post("/home/getallfileinfo").then(res => {
        if (res.data.type === "success") {
          this.tableData = res.data.content;
          console.log(this.tableData)
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      });
    },
    getAccess(index, row) {
      console.log(row)
      //私钥签名
      this.getAccessInfo.fileid = row.fileid
      this.getAccessInfo.filename = row.originalname
      var privateKey = new NodeRSA({ b: 512 });
      var str  = ""+this.getAccessInfo.fileid+this.getAccessInfo.filename+this.getAccessInfo.access+this.getAccessInfo.validity
      privateKey.importKey(window.localStorage.getItem("privateKey"));
      var signedData = privateKey
        .sign(Buffer.from(str), BASE64)
        .toString(BASE64);
        // console.log(str)
      this.getAccessInfo.signedData = signedData

      // 公钥验证签名
      var publicKey = new NodeRSA({b:512})
      publicKey.importKey("-----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJnqdYI7rO7PEZTkQFdzQ9llEPHvIumzDlNKSHW437AzuPxsBkRZxSJSdcbh4ZbmgoREP+dJI6AGJNclPGYX10sCAwEAAQ== -----END PUBLIC KEY-----")
      var result = publicKey.verify(Buffer.from(str), signedData, 'Buffer', BASE64);
      // console.log(result)

      this.$axios.post("/home/send",this.getAccessInfo).then(res=>{
        if (res.data.type === "success") {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
          this.tableData = res.data.content;
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      })

    },
    getfile(index, row) {
      this.fileInfo.fileid = row.fileid;
      this.$axios
        .post("/home/getfile", this.fileInfo, { responseType: "blob" })
        .then(res => {
          const blob = res.data;
          const reader = new FileReader();
          reader.readAsDataURL(blob);
          reader.onload = e => {
            const a = document.createElement("a");
            a.download = row.originalname;
            // 后端设置的文件名称在res.headers的 "content-disposition": "form-data; name=\"attachment\"; filename=\"20181211191944.zip\"",
            a.href = e.target.result;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
          };
        })
        .catch(err => {
         // console.log(1111)
        });
    }
  }
};
</script>
<style scoped>
.el-header {
  background-color: #6d7981;
  display: flex;
  justify-content: space-between;
  color: #ffffff;
  font-size: 20px;
  align-items: center;
}
</style>